<?php

namespace App\Admin\Controllers;

use App\Actions\ExhibitorInformationBatchTransfer;
use App\Actions\Grid\TotalRows;
use App\Imports\ExhibitorImports;
use App\Models\ExhibitorInformation;
use App\Models\SalesPersonnelCompany;
use App\Models\Unit;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Encore\Admin\Admin;

class ExhibitorInformationController extends AdminController
{
    protected $model = ExhibitorInformation::class;

    protected function title()
    {
        return '展商信息';
    }

    protected function activityList(Request $request)
    {
        $customer_type_id = $request->get('q');

        return SalesPersonnelCompany::query()->where('customer_type_id', $customer_type_id)->get(['id', 'personnel_companies as text']);
    }

    protected function grid()
    {
        $grid = new Grid(new $this->model);

        Admin::style('.box-body>.table {font-size: 11px};}');

        $grid->tools(function (Grid\Tools $tools) use ($grid) {
            $tools->append(new TotalRows($grid, '总数'));
            $tools->append(new ExhibitorImports());
        });

        $grid->batchActions(function ($batch) {
            $batch->add(new ExhibitorInformationBatchTransfer());
        });
        $grid->filter(function (Grid\Filter $filter) {
            $filter->disableIdFilter(); //去掉默认的id过滤器
            $filter->where(function ($query) {
                $keywords = $this->input;
                $query->where(function ($query) use ($keywords) {
                    $query->orWhere('name', 'like', "%{$keywords}%");;

                });
            }, trans('admin.keywords'));
            $filter->equal('booth_type', trans('exhibitor_information.booth_type'))->select(trans('exhibitor_information.booth_type_array'));
            $filter->like('area', trans('exhibitor_information.area'));
            $filter->like('discount', trans('exhibitor_information.discount'));
            $filter->equal('customer_type', trans('exhibitor_information.customer_type'))->select(trans('exhibitor_information.customer_type_array'));
            $filter->equal('signing_status', trans('exhibitor_information.signing_status'))->select(trans('exhibitor_information.signing_status_array'));
            $filter->equal('sales', trans('exhibitor_information.sales'))->select(function () {
                return SalesPersonnelCompany::query()->pluck('personnel_companies', 'id');
            });

        });

        //$grid->disableExport();

        $grid->model()->orderByDesc('id');
        $grid->column('name', trans('exhibitor_information.name'));
        $grid->column('cn_name', trans('exhibitor_information.cn_name'));
        $grid->column('journal_content.en_enterprise_full', trans('exhibitor_information.en_name'));
        $grid->column('telephone', trans('exhibitor_information.telephone'));
        $grid->column('e_mail', trans('exhibitor_information.e_mail'));
//        $grid->column('post_code', trans('exhibitor_information.post_code'));
        $grid->column('sales_personnelcompany.personnel_companies', trans('exhibitor_information.sales'));
        $grid->column('customer_type', trans('exhibitor_information.customer_type'))->using(trans('exhibitor_information.customer_type_array'));
        $grid->column('booth_number', trans('exhibitor_information.booth_number'));
        $grid->column('booth_type', trans('exhibitor_information.booth_type'));
        $grid->column('long', trans('exhibitor_information.long'));
        $grid->column('wide', trans('exhibitor_information.wide'));
        $grid->column('area', trans('exhibitor_information.area'));
        $grid->column('discount', trans('exhibitor_information.discount'));
        $grid->column('booth_fee', trans('exhibitor_information.booth_fee'));
        $grid->column('sponsorship_fees', trans('exhibitor_information.sponsorship_fees'));
        $grid->column('signing_status', trans('exhibitor_information.signing_status'))->using(trans('exhibitor_information.signing_status_array'));
        $grid->column('signing_date', trans('exhibitor_information.signing_date'));
//        $grid->column('contract', trans('exhibitor_information.contract'));
        $grid->column('pwd', trans('exhibitor_information.pwd'));
        $grid->column('created_at', trans('exhibitor_information.created_at'));
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });

        return $grid;
    }


    protected function form()
    {
        $form = new Form(new $this->model);

        $form->text('name', trans('exhibitor_information.name'))->rules('required');
        $form->text('en_name_full', trans('exhibitor_information.en_name_full'));
        $form->text('cn_name', trans('exhibitor_information.cn_name'));
        $form->text('en_name', trans('exhibitor_information.en_name'));
        $form->text('contacts', trans('exhibitor_information.contacts'));//联系人
        $form->text('appellation', trans('exhibitor_information.appellation'));//称谓
        $form->text('telephone', trans('exhibitor_information.telephone'));//电话
        $form->mobile('phone', trans('exhibitor_information.phone'));//手机号
        $form->text('e_mail', trans('exhibitor_information.e_mail'));//邮箱
        $form->text('address', trans('exhibitor_information.address'));//地址
        $form->text('post_code', trans('exhibitor_information.post_code'));//邮编

        if ($form->isEditing()) {
            $form->password('password', trans('exhibitor_information.pwd'));//登录密码
        }

        $form->select('customer_type', trans('exhibitor_information.customer_type'))->options(trans('exhibitor_information.customer_type_array'))
            ->load('sales', 'select/sales');
        $form->select('sales', trans('exhibitor_information.sales'))->options(function () {
            return SalesPersonnelCompany::query()->pluck('personnel_companies', 'id');
        });

        $form->text('remark', trans('exhibitor_information.remark'));

        $form->divider('参展信息');

        $form->text('booth_number', trans('exhibitor_information.booth_number'));
        $form->select('booth_type', trans('exhibitor_information.booth_type'))->options(trans('exhibitor_information.booth_type_array'));
        $form->number('long', trans('exhibitor_information.long'))->min(1)->max(999);
        $form->number('wide', trans('exhibitor_information.wide'))->min(1)->max(999);
        $form->decimal('area', trans('exhibitor_information.area'));
        $form->text('discount', trans('exhibitor_information.discount'));
        $form->text('booth_fee', trans('exhibitor_information.booth_fee'));
        $form->text('sponsorship_fees', trans('exhibitor_information.sponsorship_fees'));
        $form->text('papers', trans('exhibitor_information.papers'))->rules('required')->help('控制展商中心');

        $form->divider('签约\\收款\\开票信息');

        $form->file('files', trans('exhibitor_information.files'))->removable()->options([
            'maxFileSize' => 5120 // 限制图片大小5120kb
        ])->rules('mimes:pdf')->help('文件格式pdf，且不能超过5MB');
        $form->select('signing_status', trans('exhibitor_information.signing_status'))->options(trans('exhibitor_information.signing_status_array'));
        $form->datetime('signing_date', trans('exhibitor_information.signing_date'))->format('YYYY-MM-DD');
        $unit = Unit::query()->where('status', 1)->pluck('name', 'id')->toArray();
        $form->select('unit_id', trans('exhibitor_information.unit_id'))->options($unit);
        $form->select('collection_status', trans('exhibitor_information.collection_status'))->options(trans('exhibitor_information.collection_status_array'));
        $form->datetime('collection_time', trans('exhibitor_information.collection_time'))->format('YYYY-MM-DD');
        $form->select('is_invoicing', trans('exhibitor_information.is_invoicing'))->options(trans('exhibitor_information.is_invoicing_array'));
        $form->select('invoicing_status', trans('exhibitor_information.invoicing_status'))->options(trans('exhibitor_information.invoicing_status_array'));
        $form->datetime('invoicing_time', trans('exhibitor_information.invoicing_time'))->format('YYYY-MM-DD');
//        $form->text('contract', trans('exhibitor_information.contract'));
        $form->switch('status', trans('exhibitor_user.status'))->default(1);
        $form->saving(function (Form $form) {

            if ($form->isCreating()) {
                $password = 'asdf1234';
                $form->model()->pwd = $password;
                $form->model()->password = Hash::make($password);
            }
            if ($form->password) {
                $form->model()->pwd = $form->password;

            }

        });
        $form->saved(function (Form $form) {
            if ($form->isEditing()) {
                $pwd = $form->model()->pwd;
                $id = $form->model()->id;
                ExhibitorInformation::query()->find($id)->update(['password' => Hash::make($pwd)]);
//                $form->model()->password = Hash::make($pwd);
            }
        });
        $form->tools(function ($tools) {
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            $footer->disableViewCheck();
        });

        return $form;
    }

}
